<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<jsp:directive.page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" />
<jsp:directive.page import="org.apache.commons.fileupload.servlet.ServletFileUpload" />
<jsp:directive.page import="org.apache.commons.fileupload.FileItemFactory" />
<jsp:directive.page import="org.apache.commons.fileupload.FileItem" />
<jsp:directive.page import="java.io.File" /> 
<jsp:directive.page import="java.util.Calendar" /> 
			     
<jsp:directive.page import="java.util.List" />  
<jsp:directive.page import="java.util.Iterator;"/>  
  
<%
FileItemFactory factory = new DiskFileItemFactory();
	
		// Create a new file upload handler
		//ServletFileUpload
	
		ServletFileUpload upload = new ServletFileUpload(factory);
		String id = null;
	
		// Parse the request
		List items = upload.parseRequest(request);
	
		//FileItem
		// Process the uploaded items	
		Iterator iter = items.iterator();
		while (iter.hasNext()) {
			FileItem item = (FileItem) iter.next();
		    if (item.isFormField()) {
		    	if(item.getFieldName().compareTo("id")==0){
		    		id = item.getString();
		    	}
		    
			}else{			
		   	  //processUploadedFile(item);
	      		
				String fieldName = item.getFieldName();
	    		String fileName = item.getName();
			    String contentType = item.getContentType();
			    boolean isInMemory = item.isInMemory();
			    long sizeInBytes = item.getSize();
			  

			    fileName=fileName.substring(fileName.lastIndexOf("\\")+1);
			   // out.println("<br>"+fileName);
			   //  out.println("<br>" + item.getString());
			    
			    
			    
			   // out.println("fieldName = " + fieldName);
			   // out.println("fileName = " + fileName);		    
			   // out.println("contentType = " + contentType);
			   // out.println("isInMemory = " + isInMemory);
			   // out.println("sizeInBytes = " + sizeInBytes);
			    
			    
			    String savePath = getServletContext().getRealPath("/");
   				String imgUrl = null;
			    savePath += "uploadfiles";

				String timer = String.valueOf(Calendar.getInstance().getTimeInMillis());
			    
				File f = new File(savePath);
				if (!f.exists()){
					f.mkdirs();
				}
				imgUrl=timer + fileName;	
				savePath = f.getAbsolutePath()+ "/" + imgUrl;
				item.write(new File(savePath)) ;
				//out.println(imgUrl);
				
				response.sendRedirect("productAdmin.jsp?pic_path="+imgUrl+"&id="+id);
				
				
				
			
				
			
			}
			
		}		    
    
 %>
